﻿package com.dykj.platform.framework.dao.hibernate.dialect;

import com.dykj.platform.framework.dao.support.Dialect;

/**
 * @Title: DYKJ Platform
 * @Description: Oracle 方言
 * @Copyright: Copyright (c) 2008
 * @Company: DYKJ
 * @author jit@dykj.cn
 * @version 1.0
 */
public class OracleDialect implements Dialect {

	public OracleDialect() {
	}

	public String getLimitString(String sql, boolean hasOffset) {
		sql = sql.trim();
		boolean isForUpdate = false;
		if (sql.toLowerCase().endsWith(" for update")) {
			sql = sql.substring(0, sql.length() - 11);
			isForUpdate = true;
		}
		StringBuffer pagingSelect = new StringBuffer(sql.length() + 100);
		if (hasOffset)
			pagingSelect
					.append("select * from ( select row_.*, rownum rownum_ from ( ");
		else
			pagingSelect.append("select * from ( ");
		pagingSelect.append(sql);
		if (hasOffset)
			pagingSelect.append(" ) row_ where rownum <= ?) where rownum_ > ?");
		else
			pagingSelect.append(" ) where rownum <= ?");
		if (isForUpdate)
			pagingSelect.append(" for update");
		return pagingSelect.toString();
	}

	public String getLimitString(String sql) {
		return getLimitString(sql, true);
	}

	public String getSelectSequenceNextValString(String sequenceName) {
		return sequenceName + ".nextval";
	}

	public String getSequenceNextValString(String sequenceName) {
		return "select " + getSelectSequenceNextValString(sequenceName)
				+ " from dual";
	}
}
